/* 
 * @name: plugin request
 * @ahthor:sonhh<sonhh@vnext.vn> 
 * @params:request,callback
 * @return:
 * @since:2012.08.21
 */
(function($){       
            
    $.fn.request = function(request,callBack,validate){
        if($(this).data('click') == undefined){
            $(this).data('click', 1); 
        }
        
        if($(this).data('click') == 0){             
                return ;              
            }        
        $(this).data('click',0);            
        var validate = typeof validate === 'function' ? validate : null;
        var request = typeof request === 'object' ? request : {};
        // define request default        
        var default_request = {
            module : '',
            controller:'index',
            action : '',
            post : {},
            formName:'',
            cbArguments:{$this:$(this)}
        }
        
        // process validate before send request
        if(typeof validate === 'function'){
            if(validate() == false){
                $(this).data('click',1);
                return;
            }
        }
                                
        // define again params for use send request
        var params = {};
        var request = typeof request ==='object' ?  $.extend({}, default_request, request) : default_request;
        var formData =  $.extend({}, default_request.post, request.post);   
        var cbArguments = typeof request.cbArguments === 'object' ? $.extend({}, default_request.cbArguments, request.cbArguments): default_request.cbArguments;
        // neu chua khai bao action va module thi khong lam gi
        if(request.action == "" && request.module=="" ){
            return;
        }
        
        // run action 
        var url = ROOT_URL+"/index.php/"+request.module +'/'+ request.controller + '/' + request.action;         

        //  send request
        if(typeof request.formName === 'string' && request.formName != ""){

            var optionsForm = {
                type: 'POST',
                url: url,
                data: formData,
                success: function( data, textStatus, jqXHR) {
                    $(cbArguments.$this).data('click',1);
                    if( typeof callBack === 'function'){                        
                        callBack(data,cbArguments);
                    }                            
                }
            };

            if($("#"+request.formName).length > 0){
                $("#"+request.formName).ajaxForm(optionsForm);
                $("#"+request.formName).submit();
            }else{
                alert('form name is not exists');
            }                                        
        }
        else {

            $.ajax({
                type: "post",
                url: url,
                data: formData,
                success: function( data, textStatus, jqXHR ) {
                    $(cbArguments.$this).data('click',1);
                    if( typeof callBack === 'function'){
                        callBack(data,cbArguments);
                    }                        
                }
            });
        }                                                                             
    }    
    
})(jQuery)